#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 23-02-2013

@author: bart
'''
import sys
import gettext
from gi.repository import Gtk
from plotGenerator import *
from twisted.python.roots import Homogenous
from MacroButton import *

  
class mainWindow(Gtk.ApplicationWindow):
    
    def __init__(self, app):
        gettext.install('PlotGenerator', 'locale', unicode=True, names=['ngettext'])
        Gtk.Window.__init__(self, title="Plot Generator", application=app)
        self.set_default_size(640, 480)

        mainBox = Gtk.VBox(homogeneous=False, spacing=2)
        
        self.boxTools = Gtk.Box(homogeneous=False, spacing=2)
        
        self.addMacroButton(_("Test"),"Test ${kto.txt} ${kogo.txt}")
        self.addMacroButton(_("D4"),"$D4")
        self.addMacroButton(_("D6"),"$D6")
        self.addMacroButton(_("D8"),"$D8")
        self.addMacroButton(_("D10"),"$D10")
        self.addMacroButton(_("D12"),"$D12")
        self.addMacroButton(_("D20"),"$D20")
        self.addMacroButton(_("D100"),"$D100")
        
        
        mainBox.pack_start(self.boxTools, False, True, 2)
        
        box1 = Gtk.Box(homogeneous=False, spacing=5)
        label1 = Gtk.Label(_("Generate"))
        box1.pack_start(label1, True, True, 5)
        
        self.entryInput = Gtk.Entry()
        self.entryInput.set_text("${kto.txt} ${kogo.txt} ${co_robi.txt} ${dlaczego.txt}")
        box1.pack_start(self.entryInput, True, True, 5)
        
        buttonGenerate = Gtk.Button(_("Generate"))
        buttonGenerate.connect("clicked", self.buttonGenerate_clicked )
        box1.pack_end(buttonGenerate, True, True, 5)
        
        mainBox.pack_start(box1, False, True, 2)
        scrolled_window = Gtk.ScrolledWindow()
        scrolled_window.set_border_width(5)
        scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)

        
        self.bufferOut = Gtk.TextBuffer()

        # a textview (displays the buffer)
        self.textview = Gtk.TextView(buffer=self.bufferOut)
        # wrap the text, if needed, breaking lines in between words
        self.textview.set_wrap_mode(Gtk.WrapMode.WORD)

        # textview is scrolled
        scrolled_window.add(self.textview)
        mainBox.add(scrolled_window)
        self.add(mainBox)
        
    def addMacroButton(self, label, macro):
        button = MacroButton(label, macro)
        button.connect("clicked", self.buttonMacro_clicked,button.macro)
        self.boxTools.pack_start(button, True, True, 5)
        
    def buttonMacro_clicked(self, event, data):
        self.bufferOut.insert(self.bufferOut.get_end_iter(), event.get_label() + ": ")
        self.generatePlot(data)
        
    def buttonGenerate_clicked(self,event):
        text = self.entryInput.get_text()
        self.generatePlot(text)        
        
    def generatePlot(self, text):
        pg = plotGenerator()
        self.bufferOut.insert(self.bufferOut.get_end_iter() , pg.parseLine(text) + '\n') 
        self.textview.scroll_mark_onscreen(self.bufferOut.get_insert())       
        
class gtkPlotGeneratorGUI(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = mainWindow(self)
        win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)
        
        
if __name__ == '__main__':
    pg = gtkPlotGeneratorGUI()
    exit_status = pg.run(sys.argv)
    sys.exit(exit_status)
